package com.nbcio.modules.erp.voucher.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.erp.voucher.domain.ErpAccountConfig;
import com.nbcio.modules.erp.voucher.service.IErpAccountConfigService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科目配置控制器
 */
@RestController
@RequestMapping("/erp/account/config")
public class ErpAccountConfigController {

    @Resource
    private IErpAccountConfigService configService;

    /**
     * 分页查询配置列表
     */
    @GetMapping("/page")
    public R<Page<ErpAccountConfig>> page(
            @RequestParam(required = false) String billType,
            @RequestParam(required = false) String entryType,
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize) {
        LambdaQueryWrapper<ErpAccountConfig> qw = new LambdaQueryWrapper<>();
        if (billType != null && !billType.isEmpty()) {
            qw.eq(ErpAccountConfig::getBillType, billType);
        }
        if (entryType != null && !entryType.isEmpty()) {
            qw.eq(ErpAccountConfig::getEntryType, entryType);
        }
        qw.orderByDesc(ErpAccountConfig::getPriority);
        
        Page<ErpAccountConfig> page = new Page<>(pageNum, pageSize);
        return R.ok(configService.page(page, qw));
    }

    /**
     * 查询配置列表
     */
    @GetMapping("/list")
    public R<List<ErpAccountConfig>> list(
            @RequestParam(required = false) String billType) {
        LambdaQueryWrapper<ErpAccountConfig> qw = new LambdaQueryWrapper<>();
        if (billType != null && !billType.isEmpty()) {
            qw.eq(ErpAccountConfig::getBillType, billType);
        }
        qw.eq(ErpAccountConfig::getStatus, 1);
        qw.orderByDesc(ErpAccountConfig::getPriority);
        return R.ok(configService.list(qw));
    }

    /**
     * 根据ID获取配置
     */
    @GetMapping("/{id}")
    public R<ErpAccountConfig> get(@PathVariable("id") String id) {
        return R.ok(configService.getById(Long.parseLong(id)));
    }

    /**
     * 获取配置
     */
    @GetMapping("/get")
    public R<ErpAccountConfig> getConfig(
            @RequestParam String billType,
            @RequestParam String entryType) {
        return R.ok(configService.getConfig(billType, entryType));
    }

    /**
     * 新增配置
     */
    @PostMapping
    public R<Boolean> create(@RequestBody ErpAccountConfig config) {
        return R.ok(configService.save(config));
    }

    /**
     * 更新配置
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody ErpAccountConfig config) {
        config.setConfigId(Long.parseLong(id));
        return R.ok(configService.updateById(config));
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") String id) {
        return R.ok(configService.removeById(Long.parseLong(id)));
    }
}

